const {
    parseStudentId
} = require('../../utils/helpers.js');

module.exports = async (ctx, next) => {
    const { model, id } = ctx.params;
    let oldName;
    if(model==='student'){
        const student = await strapi.services.student.fetch({id});
        oldName= student.name;
    }
    await next();
    if(ctx.params.model==='student' && ctx.response.status===200 && oldName!==ctx.response.body.name){
        const { id, name, device} = ctx.response.body;
        const data = {
            device:device.id,
            action:'student-edit',
            data:{
                user_id:parseStudentId(id),
                user_name:name,
            }
        };
        await strapi.plugins['content-manager'].services['contentmanager'].add({ model: 'task' }, data, 'task');
    }
};
